//
//  FeileiViewController.m
//  BabyMusic
//
//  Created by Tayoji on 15-6-11.
//  Copyright (c) 2015年 Tayoji. All rights reserved.
//

#import "FeileiViewController.h"
#import "DataModel.h"
#import "CatetoryCell.h"
@interface FeileiViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>
{
    AFHTTPRequestOperationManager * _manager;
    UICollectionView * _collectionView;
    NSMutableArray * _dataArr;
}
@property(nonatomic,strong)UICollectionView  *collectionView;
@property(nonatomic,strong)NSMutableArray * dataArr;
@end

@implementation FeileiViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self creatViewAndRequset];
    [self firstDown];
}
-(void)creatViewAndRequset{
    self.dataArr=[[NSMutableArray alloc] init];
    UICollectionViewFlowLayout * layout =[[UICollectionViewFlowLayout alloc] init];
    CGFloat  space=(kSreenSize.width-45)/2;
    layout.itemSize=CGSizeMake(space, space+20);
    layout.sectionInset=UIEdgeInsetsMake(15, 15, 15, 15);

    
    _collectionView=[[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, kSreenSize.width, kSreenSize.height-64-55) collectionViewLayout:layout];
    self.collectionView.dataSource=self;
    self.collectionView.delegate=self;
    self.collectionView.backgroundColor=[UIColor colorWithRed:232/255.f green:232/255.f blue:232/255.f alpha:1.0];
    [self.collectionView registerClass:[CatetoryCell class] forCellWithReuseIdentifier:@"CatetoryCell"];
    [self.view addSubview:self.collectionView];
    _manager=[AFHTTPRequestOperationManager manager];
    _manager.responseSerializer=[AFHTTPResponseSerializer serializer];
}
-(void)firstDown{
    [self downloadWithStart:0 end:16];
}
-(void)downloadWithStart:(NSInteger)start end:(NSInteger)end{
    __weak typeof(self)weakSelf=self;
    NSString * str =[NSString stringWithFormat:kCategory,start,end];
    [_manager GET:str parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        if (responseObject) {
            NSDictionary * dict=[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
            NSDictionary * dataDict=dict[@"data"];
            DataModel *model=[[DataModel alloc] initWithDictionary:dataDict error:nil];
            NSArray * arr=model.catetoryList;
            for (CatetoryListModel * listModel in arr) {
                [weakSelf.dataArr addObject:listModel];
            }
            [weakSelf.collectionView reloadData];
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"分类下载失败");
    }];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.dataArr.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    CatetoryCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"CatetoryCell" forIndexPath:indexPath];
    CatetoryListModel *model=self.dataArr[indexPath.row];

    [cell showDataWithModel:model];
    return cell;
}

-(void)viewWillShow{

}
@end
